home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / depth_buffer / polygon_offset.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  3.6 KB  |  157 lines

  1. /* Copyright 1996 Silicon Graphics, Inc.
  2.  * All Rights Reserved.
  3.  *
  4.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  5.  * the contents of this file may not be disclosed to third parties, copied or
  6.  * duplicated in any form, in whole or in part, without the prior written
  7.  * permission of Silicon Graphics, Inc.
  8.  *
  9.  * RESTRICTED RIGHTS LEGEND:
  10.  * Use, duplication or disclosure by the Government is subject to restrictions
  11.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  12.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  13.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  14.  * rights reserved under the Copyright Laws of the United States.
  15.  */
  16.  
  17. /* polygon_offset.c - draw a solid polygon with an outline.
  18.  *
  19.  *    <p> Key        - toggle polygon offset extension on/off
  20.  *    Escape Key    - exit program
  21.  */
  22. #include <GL/gl.h>
  23. #include <GL/glu.h>
  24. #include <GL/glut.h>
  25.  
  26. #include <stdio.h>
  27.  
  28. /*  Function Prototypes  */
  29.  
  30. GLvoid  initgfx( GLvoid );
  31. GLvoid  keyboard( GLubyte, GLint, GLint );
  32. GLvoid  drawScene( GLvoid );
  33. GLvoid  reshape( GLsizei, GLsizei );
  34.  
  35. void printHelp( char * );
  36.  
  37. /* Global Definitions */
  38.  
  39. #define KEY_ESC    27    /* ascii value for the escape key */
  40.  
  41. void
  42. main( int argc, char *argv[] )
  43. {
  44.     GLsizei width, height;
  45.  
  46.     glutInit( &argc, argv );
  47.  
  48.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  49.     height = glutGet( GLUT_SCREEN_HEIGHT );
  50.     glutInitWindowPosition( (width / 2) + 4, height / 4 );
  51.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  52.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH );
  53.     glutCreateWindow( argv[0] );
  54.  
  55.     initgfx();
  56.  
  57.     glutReshapeFunc( reshape );
  58.     glutKeyboardFunc( keyboard );
  59.     glutDisplayFunc( drawScene ); 
  60.  
  61.     printHelp( argv[0] );
  62.  
  63.     glutMainLoop();
  64. }
  65.  
  66. void
  67. printHelp( char *progname )
  68. {
  69.     fprintf(stdout, "\n%s - demonstrates depth buffering\n\n"
  70.         "<p> Key        - toggle polygon offset on/off\n"
  71.         "Escape Key        - exit the program\n\n",
  72.         progname);
  73. }
  74.  
  75. GLvoid
  76. initgfx( GLvoid )
  77. {
  78.     glClearColor( 0.0, 0.0, 0.5, 1.0 );
  79.     glShadeModel( GL_FLAT );
  80.  
  81.     glEnable( GL_DEPTH_TEST );
  82.  
  83.     glLineWidth( 2.5 );
  84.  
  85. #ifdef    GL_EXT_polygon_offset
  86.     if ( glutExtensionSupported( "GL_EXT_polygon_offset" )) {
  87.         glPolygonOffsetEXT( 1.0, 0.00002 );
  88.     }
  89. #endif
  90. }
  91.  
  92. GLvoid
  93. reshape( GLsizei width, GLsizei height )
  94. {
  95.     GLdouble    aspect;
  96.  
  97.     glViewport( 0, 0, width, height );
  98.  
  99.     glMatrixMode( GL_PROJECTION );
  100.     glLoadIdentity();
  101.     aspect = (GLdouble) width / (GLdouble) height;
  102.     gluPerspective( 45.0, aspect, 3.0, 7.0 );
  103.     glMatrixMode( GL_MODELVIEW );
  104. }
  105.  
  106. GLvoid 
  107. keyboard( GLubyte key, GLint x, GLint y )
  108. {
  109.     static GLboolean offsetPolygon = GL_FALSE;
  110.     switch (key) {
  111.     case 'p':
  112. #ifdef    GL_EXT_polygon_offset
  113.         offsetPolygon = !offsetPolygon;
  114.         if ( offsetPolygon )
  115.             glEnable( GL_POLYGON_OFFSET_EXT );
  116.         else
  117.             glDisable( GL_POLYGON_OFFSET_EXT );
  118.         printf("polygon offset %s\n", 
  119.             (offsetPolygon? "Enabled":"Disabled"));
  120. #else
  121.         printf("polygon offset not supported by client\n"); 
  122. #endif
  123.         glutPostRedisplay();
  124.         break;
  125.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  126.         exit(0);
  127.     }
  128. }
  129.  
  130. GLvoid
  131. drawScene( GLvoid )
  132. {
  133.     static GLfloat    fillColor[] = { 1.0, 1.0, 1.0 };
  134.     static GLfloat    outlineColor[] = { 0.0, 0.0, 0.0 };
  135.  
  136.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  137.  
  138.     glPushMatrix();
  139.         /* Move origin between the near and far clipping planes */
  140.         glTranslatef( 0.0, 0.0, -5.0 );
  141.         
  142.         glRotatef( 45.0, 1.0, 1.0, 1.0 );
  143.  
  144.         /* Draw a solid sphere with an outline */
  145.         glColor3fv( fillColor );
  146.         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  147.         glutSolidSphere( 1.5, 15, 15 );
  148.  
  149.         glColor3fv( outlineColor );
  150.         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  151.         glutSolidSphere( 1.5, 15, 15 );
  152.  
  153.     glPopMatrix();
  154.  
  155.     glFlush();
  156. }
  157.